package ar.com.larctest.forms;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import ar.com.larctest.R;
import ar.com.larctest.tasks.UpdateHorses;
import ar.com.larctest.utils.Constants;

public class MenuActivity extends Activity {
    private static final String TAG = "MenuActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
    }

    public void onClickLoadTest(View view) {
        Intent intentMeasurement = new Intent(this, MeasurementSearchActivity.class);
        startActivityForResult(intentMeasurement, 1);
    }

    public void onClickHorsesData(View view) {
        Intent intentHorses = new Intent(this, HorsesActivity.class);
        startActivityForResult(intentHorses, 1);
    }

    public void onClickSyncData(View view) {
        myHandler.sendEmptyMessage(Constants.TASK_NOTHING);
    }

    public void onClickExit(View view)
    {
        AlertDialog.Builder alertDialogExit = new AlertDialog.Builder(this);
        alertDialogExit.setMessage(getString(R.string.exitAppMsg))
                .setCancelable(false)
                .setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        MenuActivity.this.finish();
                    }
                })
                .setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        alertDialogExit.create();
        alertDialogExit.show();
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            try
            {
                switch (msg.what) {
                    case Constants.TASK_NOTHING:
                        new UpdateHorses(MenuActivity.this, myHandler).execute();
                        break;

                    //Last Task
                    case Constants.TASK_SYNC_HORSES:
                        LayoutInflater inflater = getLayoutInflater();
                        View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));

                        TextView text = (TextView) (layout != null ? layout.findViewById(R.id.text) : null);
                        if (text != null) {
                            text.setText(getText(R.string.syncCommited));
                        }

                        Toast toast = new Toast(getApplicationContext());
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.setDuration(Toast.LENGTH_LONG);
                        toast.setView(layout);
                        toast.show();
                        break;
                }
            }
            catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    };
}
